package com.asset.dispoal.business.mediation.service.common;

import com.asset.dispoal.business.mediation.domain.db.entity.CollectionRecord;
import com.asset.dispoal.business.mediation.domain.db.mapper.CollectionRecordMapper;
import com.asset.dispoal.business.mediation.dto.CollectionRecordResponse;
import com.asset.dispoal.business.mediation.dto.QueryCollectionRecordRequest;
import com.asset.dispoal.business.mediation.service.converter.CollectionRecordConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-13 12:31:50
 */
@Repository
public class QueryColletionCommand implements BaseCommand<QueryCollectionRecordRequest, Result<PageVO<CollectionRecordResponse>>> {
    @Autowired
    private CollectionRecordMapper recordMapper;

    @Override
    public Result<PageVO<CollectionRecordResponse>> execute(QueryCollectionRecordRequest request) {
        CollectionRecord record = CollectionRecordConverter.INSTANCE.qReqToDo(request);
        int count = recordMapper.count(record);
        Page<CollectionRecord> page = PagingQuery.getPage(count, record);
        PageVO<CollectionRecordResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);

        if (page != null) {
            List<CollectionRecord> result = recordMapper.queryAllByLimit(page);
            List<CollectionRecordResponse> responses = CollectionRecordConverter.INSTANCE.doToRes(result);
            pageVO.setResult(responses);
        }
        return Result.ok(pageVO);
    }
}
